package yannan.voiplock4ble.view;

import android.content.Context;
import android.os.Build;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

import io.github.yavski.fabspeeddial.FabSpeedDial;
import yannan.voiplock4ble.BuildConfig;
import yannan.voiplock4ble.MainActivity;
import yannan.voiplock4ble.R;
import yannan.voiplock4ble.utils.LogUtils;

/**
 * 说明：FAB
 * 作者：mainTel
 * 时间：2016/11/21 17:34
 * 备注：
 */
public class ScrollAwareFABBehaviorDefault extends CoordinatorLayout.Behavior<FabSpeedDial> implements MainActivity.IOnMenuChangeLisenter {

    //定义toolbar高度和状态栏高度
    private int toolbarHeight;
    private double statusBarHeight;

    public ScrollAwareFABBehaviorDefault() {
    }

    public ScrollAwareFABBehaviorDefault(Context context, AttributeSet attrs) {
        super(context, attrs);

        //得到两个高度值
        this.toolbarHeight = getToolbarHeight(context);
        this.statusBarHeight = getStatusBarHeight(context);
    }

    private int i = 0;


    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, FabSpeedDial child, View directTargetChild, View target, int nestedScrollAxes) {

        if (i == 0) {
            MainActivity.getMainActivity().setIOnMenuChangeLisenter(this);
            i++;
        }

        return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL
                || super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, FabSpeedDial fab, View dependency) {
        //设定依赖的父布局为AppBarLayout
        return dependency instanceof AppBarLayout;
    }


    @Override
    public void onNestedScroll(CoordinatorLayout coordinatorLayout, FabSpeedDial child,
                               View target, int dxConsumed, int dyConsumed,
                               int dxUnconsumed, int dyUnconsumed) {
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);

        if (isNeedVisible)
            if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) {
                // User scrolled down and the FAB is currently visible -> hide the FAB
                child.hide();
            } else if (dyConsumed < 0) {
                // User scrolled up and the FAB is currently not visible -> show the FAB
                child.show();
            }
    }


//
//    @Override
//    public boolean onDependentViewChanged(CoordinatorLayout parent, FabSpeedDial fab, View dependency) {
//        if (dependency instanceof AppBarLayout) {
//            //FAB的自身高度+据底边高度的和=要实现FAB隐藏需要向下移动的距离
//            CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
//            int fabBottomMargin = lp.bottomMargin;
//            int distanceToScroll = fab.getHeight() + fabBottomMargin;
//            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) {
//                //toolbar被移走的距离与本身高度的比值
//                float ty = dependency.getY() - (float) statusBarHeight;
//                float ratio = ty / (float) toolbarHeight;
//                //toolbar被移走几分之几，fab就向下滑几分之几
//                fab.setTranslationY(-distanceToScroll * ratio);
//            }
//        }
//        return true;
//    }

    private int getToolbarHeight(Context context) {
        TypedValue tv = new TypedValue();
        int actionBarHeight = android.support.v7.appcompat.R.attr.actionBarSize;
        if (context.getTheme().resolveAttribute(R.attr.actionBarSize, tv, true)) {
            actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,
                    context.getResources().getDisplayMetrics());
        }

        return actionBarHeight;
    }

    //状态栏高度的方法
    private int getStatusBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }


    @Override
    public void ChangeMenu(boolean key) {
        isNeedVisible = key;
        LogUtils.i("是否需要显示" + key);
    }

    public static boolean isNeedVisible = false;


}